<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>三/五/零之法则</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/rule_of_three","wgTitle":"cpp/language/rule of three","wgCurRevisionId":65073,"wgArticleId":8460,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/rule_of_three","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_rule_of_three skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">三/五/零之法则</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> 一般主题</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="../preprocessor.html" title="cpp/preprocessor"> 预处理器</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../comments.html" title="cpp/comment"> 注释</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="../keywords.html" title="cpp/keyword"> 关键词</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="escape.html" title="cpp/language/escape"> 转义序列</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 流程控制</td></tr>
<tr class="t-nv-h2"><td colspan="5"> 条件执行语句</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="if.html" title="cpp/language/if"> <code>if</code></a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="switch.html" title="cpp/language/switch"> <code>switch</code></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 重复语句（循环）</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="for.html" title="cpp/language/for"> <code>for</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="range-for.html" title="cpp/language/range-for"> 范围 <code>for</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="while.html" title="cpp/language/while"> <code>while</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="do.html" title="cpp/language/do"> <code>do-while</code></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 跳转语句 </td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="continue.html" title="cpp/language/continue"> <code>continue</code></a> - <a href="break.html" title="cpp/language/break"> <code>break</code></a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="goto.html" title="cpp/language/goto"> <code>goto</code></a> - <a href="return.html" title="cpp/language/return"> <code>return</code></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 函数</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda"> Lambda 函数声明</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="inline.html" title="cpp/language/inline"> <code>inline</code> 说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="except_spec.html" title="cpp/language/except spec"> 异常规定</a> <span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept_spec.html" title="cpp/language/noexcept spec"> <code>noexcept</code> 说明符</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 异常</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw"> <code>throw</code> 表达式</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="try_catch.html" title="cpp/language/try catch"> <code>try</code>-<code>catch</code> 块</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 命名空间</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间声明</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 类型</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="types.html" title="cpp/language/types"> 基础类型</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 枚举类型</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数类型</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class.html" title="cpp/language/class"> 复合类型</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="union.html" title="cpp/language/union"> 联合体类型</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="decltype.html" title="cpp/language/decltype"> <code>decltype</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="auto.html" title="cpp/language/auto"> <code>auto</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignas.html" title="cpp/language/alignas"> <code>alignas</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="cv.html" title="cpp/language/cv"> <code>const</code>/<code>volatile</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constexpr.html" title="cpp/language/constexpr"> <code>constexpr</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储期说明符</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 初始化</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="default_initialization.html" title="cpp/language/default initialization"> 默认初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_initialization.html" title="cpp/language/value initialization"> 值初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="zero_initialization.html" title="cpp/language/zero initialization"> 零初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="copy_initialization.html" title="cpp/language/copy initialization"> 复制初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="direct_initialization.html" title="cpp/language/direct initialization"> 直接初始化</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="aggregate_initialization.html" title="cpp/language/aggregate initialization"> 聚合初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="list_initialization.html" title="cpp/language/list initialization"> 列表初始化</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constant_initialization.html" title="cpp/language/constant initialization"> 常量初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference_initialization.html" title="cpp/language/reference initialization"> 引用初始化</a></td></tr>
</table></div></td></tr>
</table></div>
</div>
<div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> 表达式</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> 值类别</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="cpp/language/eval order"> 求值顺序</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators"> 运算符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence"> 运算符优先级</a></td></tr>
</table></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative"> 替代表示</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 字面量</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal"> 布尔</a> - <a href="integer_literal.html" title="cpp/language/integer literal"> 整数</a> - <a href="floating_literal.html" title="cpp/language/floating literal"> 浮点</a></td></tr>   
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal"> 字符</a> - <a href="string_literal.html" title="cpp/language/string literal"> 字符串</a> - <a href="nullptr.html" title="cpp/language/nullptr"> <code>nullptr</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr> 
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal"> 用户定义</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 工具</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 类型</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typedef.html" title="cpp/language/typedef"> <code>typedef</code> 声明</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 类型别名声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 类型转换</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion"> 隐式转换</a> - <a href="explicit_cast.html" title="cpp/language/explicit cast"> 显式转换</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast"> <code>static_cast</code></a> - <a href="dynamic_cast.html" title="cpp/language/dynamic cast"> <code>dynamic_cast</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast"> <code>const_cast</code></a> - <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"> <code>reinterpret_cast</code></a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 内存分配</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new"> <code>new</code> 表达式</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete"> <code>delete</code> 表达式</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 类</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class.html" title="cpp/language/class"> 类声明</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constructor.html" title="cpp/language/initializer list"> 初始化器列表</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="this.html" title="cpp/language/this"> <code>this</code> 指针</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="access.html" title="cpp/language/access"> 访问说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="friend.html" title="cpp/language/friend"> <code>friend</code> 说明符</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 特定于类的函数性质</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> 虚函数</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="override.html" title="cpp/language/override"> <code>override</code> 指定符</a>  <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="final.html" title="cpp/language/final"> <code>final</code> 说明符</a>  <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> <code>explicit</code></a>  <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static.html" title="cpp/language/static"> <code>static</code></a> </td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 特殊成员函数</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="default_constructor.html" title="cpp/language/default constructor"> 默认构造函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="copy_constructor.html" title="cpp/language/copy constructor"> 复制构造函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="move_constructor.html" title="cpp/language/move constructor"> 移动构造函数</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="copy_assignment.html" title="cpp/language/copy assignment"> 复制赋值</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="move_assignment.html" title="cpp/language/move assignment"> 移动赋值</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="destructor.html" title="cpp/language/destructor"> 析构函数</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 模板</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 模板特化</a></td></tr>   
<tr class="t-nv"><td colspan="5"> <a href="parameter_pack.html" title="cpp/language/parameter pack"> 参数包</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> 内联汇编</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="history.html" title="cpp/language/history"> C++ 的历史</a></td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<h3><span class="mw-headline" id=".E4.B8.89.E4.B9.8B.E6.B3.95.E5.88.99">三之法则</span></h3>
<p>若某个类需要用户定义的<a href="destructor.html" title="cpp/language/destructor">析构函数</a>、用户定义的<a href="copy_constructor.html" title="cpp/language/copy constructor">复制构造函数</a>或用户定义的<a href="copy_assignment.html" title="cpp/language/copy assignment">复制赋值运算符</a>，则它几乎肯定三者全部都需要。
</p><p>因为 C++ 在各种场合（按值传递/返回、操纵容器等）对对象进行复制和复制赋值，若可访问，这些特殊成员函数就会被调用，而且若用户不定义他们，则编译器会隐式定义。
</p><p>如果类对某种资源进行管理，而资源句柄是非类类型的对象（裸指针、POSIX 文件描述符等），则这些隐式定义的成员函数通常都不正确，其析构函数不做任何事，而复制构造函数/复制赋值运算符则进行“浅复制”（复制句柄的值，而不复制底层资源）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> rule_of_three
<span class="br0">{</span>
    <span class="kw4">char</span><span class="sy2">*</span> cstring<span class="sy4">;</span> <span class="co1">// 以裸指针为动态分配内存的句柄</span>
 
    <span class="kw4">void</span> init<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> s<span class="br0">)</span>
    <span class="br0">{</span>
        <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n <span class="sy1">=</span> <a href="../string/byte/strlen.html"><span class="kw1145">std::<span class="me2">strlen</span></span></a><span class="br0">(</span>s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span>
        cstring <span class="sy1">=</span> new <span class="kw4">char</span><span class="br0">[</span>n<span class="br0">]</span><span class="sy4">;</span>
        <a href="../string/byte/memcpy.html"><span class="kw1159">std::<span class="me2">memcpy</span></span></a><span class="br0">(</span>cstring, s, n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 填充</span>
    <span class="br0">}</span>
 <span class="kw1">public</span><span class="sy4">:</span>
    rule_of_three<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> s <span class="sy1">=</span> <span class="st0">""</span><span class="br0">)</span> <span class="br0">{</span> init<span class="br0">(</span>s<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
 
    ~rule_of_three<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
        delete<span class="br0">[</span><span class="br0">]</span> cstring<span class="sy4">;</span>  <span class="co1">// 解分配</span>
    <span class="br0">}</span>
 
    rule_of_three<span class="br0">(</span><span class="kw4">const</span> rule_of_three<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="co1">// 复制构造函数</span>
    <span class="br0">{</span> 
        init<span class="br0">(</span>other.<span class="me1">cstring</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="br0">}</span>
 
    rule_of_three<span class="sy3">&amp;</span> operator<span class="sy1">=</span><span class="br0">(</span><span class="kw4">const</span> rule_of_three<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="co1">// 复制赋值</span>
    <span class="br0">{</span>
        <span class="kw1">if</span><span class="br0">(</span>this <span class="sy3">!</span><span class="sy1">=</span> <span class="sy3">&amp;</span>other<span class="br0">)</span> <span class="br0">{</span>
            delete<span class="br0">[</span><span class="br0">]</span> cstring<span class="sy4">;</span>  <span class="co1">// 解分配</span>
            init<span class="br0">(</span>other.<span class="me1">cstring</span><span class="br0">)</span><span class="sy4">;</span>
        <span class="br0">}</span>
        <span class="kw1">return</span> <span class="sy2">*</span>this<span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>通过可复制句柄来管理不可复制资源的类，可能必须将其复制赋值和复制构造函数声明为私有的并且不提供其定义，或将它们定义为弃置的。这是三之法则的另一种应用：删除其中之一而遗留其他被隐式定义很可能会导致错误。
</p>
<h3><span class="mw-headline" id=".E4.BA.94.E4.B9.8B.E6.B3.95.E5.88.99">五之法则</span></h3>
<p>因为用户定义析构函数、复制构造函数或复制赋值运算符的存在阻止<a href="move_constructor.html" title="cpp/language/move constructor">移动构造函数</a>和<a href="move_assignment.html" title="cpp/language/move assignment">移动赋值运算符</a>的隐式定义，所以任何想要移动语义的类必须声明全部五个特殊成员函数：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> rule_of_five
<span class="br0">{</span>
    <span class="kw4">char</span><span class="sy2">*</span> cstring<span class="sy4">;</span> <span class="co1">// 以裸指针为动态分配内存的句柄</span>
 <span class="kw1">public</span><span class="sy4">:</span>
    rule_of_five<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> s <span class="sy1">=</span> <span class="st0">""</span><span class="br0">)</span>
    <span class="sy4">:</span> cstring<span class="br0">(</span>nullptr<span class="br0">)</span>
    <span class="br0">{</span> 
        <span class="kw1">if</span> <span class="br0">(</span>s<span class="br0">)</span> <span class="br0">{</span>
            <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n <span class="sy1">=</span> <a href="../string/byte/strlen.html"><span class="kw1145">std::<span class="me2">strlen</span></span></a><span class="br0">(</span>s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span>
            cstring <span class="sy1">=</span> new <span class="kw4">char</span><span class="br0">[</span>n<span class="br0">]</span><span class="sy4">;</span>      <span class="co1">// 分配</span>
            <a href="../string/byte/memcpy.html"><span class="kw1159">std::<span class="me2">memcpy</span></span></a><span class="br0">(</span>cstring, s, n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 填充</span>
        <span class="br0">}</span> 
    <span class="br0">}</span>
 
    ~rule_of_five<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
        delete<span class="br0">[</span><span class="br0">]</span> cstring<span class="sy4">;</span>  <span class="co1">// 解分配</span>
    <span class="br0">}</span>
 
    rule_of_five<span class="br0">(</span><span class="kw4">const</span> rule_of_five<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="co1">// 复制构造函数</span>
    <span class="sy4">:</span> rule_of_five<span class="br0">(</span>other.<span class="me1">cstring</span><span class="br0">)</span>
    <span class="br0">{</span><span class="br0">}</span>
 
    rule_of_five<span class="br0">(</span>rule_of_five<span class="sy3">&amp;&amp;</span> other<span class="br0">)</span> <span class="kw1">noexcept</span> <span class="co1">// 移动构造函数</span>
    <span class="sy4">:</span> cstring<span class="br0">(</span><a href="../utility/exchange.html"><span class="kw1118">std::<span class="me2">exchange</span></span></a><span class="br0">(</span>other.<span class="me1">cstring</span>, nullptr<span class="br0">)</span><span class="br0">)</span>
    <span class="br0">{</span><span class="br0">}</span>
 
    rule_of_five<span class="sy3">&amp;</span> operator<span class="sy1">=</span><span class="br0">(</span><span class="kw4">const</span> rule_of_five<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="co1">// 复制赋值</span>
    <span class="br0">{</span>
         <span class="kw1">return</span> <span class="sy2">*</span>this <span class="sy1">=</span> rule_of_five<span class="br0">(</span>other<span class="br0">)</span><span class="sy4">;</span>
    <span class="br0">}</span>
 
    rule_of_five<span class="sy3">&amp;</span> operator<span class="sy1">=</span><span class="br0">(</span>rule_of_five<span class="sy3">&amp;&amp;</span> other<span class="br0">)</span> <span class="kw1">noexcept</span> <span class="co1">// 移动赋值</span>
    <span class="br0">{</span>
        <a href="../algorithm/swap.html"><span class="kw1599">std::<span class="me2">swap</span></span></a><span class="br0">(</span>cstring, other.<span class="me1">cstring</span><span class="br0">)</span><span class="sy4">;</span>
        <span class="kw1">return</span> <span class="sy2">*</span>this<span class="sy4">;</span>
    <span class="br0">}</span>
 
<span class="co1">// 另一种方法是用以下函数替代两个赋值运算符</span>
<span class="co1">//  rule_of_five&amp; operator=(rule_of_five other) noexcept</span>
<span class="co1">//  {</span>
<span class="co1">//      std::swap(cstring, other.cstring);</span>
<span class="co1">//      return *this;</span>
<span class="co1">//  }</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>与三之法则不同的是，不提供移动构造函数和移动赋值运算符通常不是错误，但会导致失去优化机会。
</p>
<h3><span class="mw-headline" id=".E9.9B.B6.E4.B9.8B.E6.B3.95.E5.88.99">零之法则</span></h3>
<p>有自定义析构函数、复制/移动构造函数或复制/移动赋值运算符的类应该专门处理所有权（这遵循<a href="https://en.wikipedia.org/wiki/Single_responsibility_principle" class="extiw" title="enwiki:Single responsibility principle">单一责任原则</a>）。其他类都不应该拥有自定义的析构函数、复制/移动构造函数或复制/移动赋值运算符<sup id="cite_ref-1" class="reference"><a href="rule_of_three.html#cite_note-1">[1]</a></sup>
</p><p>这条法则也在 C++ 核心指南（C++ Core Guidelines）中出现，为 <a rel="nofollow" class="external text" href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-zero">C.20：一旦可以避免定义默认操作就应当施行</a>。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> rule_of_zero
<span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> cppstring<span class="sy4">;</span>
 <span class="kw1">public</span><span class="sy4">:</span>
    rule_of_zero<span class="br0">(</span><span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> arg<span class="br0">)</span> <span class="sy4">:</span> cppstring<span class="br0">(</span>arg<span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>当有意将某个基类用于多态用途时，可能必须将其析构函数声明为公开的虚函数。由于这会阻拦隐式移动（并弃用隐式复制）的生成，因而必须将各特殊成员函数声明为预置的<sup id="cite_ref-2" class="reference"><a href="rule_of_three.html#cite_note-2">[2]</a></sup>
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> base_of_five_defaults
<span class="br0">{</span>
 <span class="kw1">public</span><span class="sy4">:</span>
    base_of_five_defaults<span class="br0">(</span><span class="kw4">const</span> base_of_five_defaults<span class="sy3">&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    base_of_five_defaults<span class="br0">(</span>base_of_five_defaults<span class="sy3">&amp;&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    base_of_five_defaults<span class="sy3">&amp;</span> operator<span class="sy1">=</span><span class="br0">(</span><span class="kw4">const</span> base_of_five_defaults<span class="sy3">&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    base_of_five_defaults<span class="sy3">&amp;</span> operator<span class="sy1">=</span><span class="br0">(</span>base_of_five_defaults<span class="sy3">&amp;&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    <span class="kw1">virtual</span> ~base_of_five_defaults<span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>然而这使得类有可能被切片，这是多态类经常把复制定义为弃置的原因（见 C++ 核心指南中的 <a rel="nofollow" class="external text" href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-copy-virtual">C.67：多态类应该抑制复制操作</a>），这带来了下列的五之法则的通用说法：
</p>
<dl><dd><a rel="nofollow" class="external text" href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-five">C.21：若有任何默认操作被定义为 =delete，则应当对它们全部进行定义或 =delete</a>
</dd></dl>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><a href="rule_of_three.html#cite_ref-1">↑</a></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://rmf.io/cxx11/rule-of-zero">“零之法则”，R. Martinho Fernandes 8/15/2012</a></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><a href="rule_of_three.html#cite_ref-2">↑</a></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://scottmeyers.blogspot.fr/2014/03/a-concern-about-rule-of-zero.html">“关于零之法则的考虑”，Scott Meyers, 3/13/2014</a></span>
</li>
</ol>

<!-- 
NewPP limit report
Preprocessor visited node count: 1499/1000000
Preprocessor generated node count: 4325/1000000
Post‐expand include size: 99019/2097152 bytes
Template argument size: 9879/2097152 bytes
Highest expansion depth: 15/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:8460-0!*!0!!zh!*!zh!* and timestamp 20200223161455 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/rule_of_three&amp;oldid=65073">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/rule_of_three&amp;oldid=65073</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/rule_of_three">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年2月23日 (星期日) 08:14。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过5,194次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.074 secs. -->
	</body>
<!-- Cached 20200223161504 -->
</html>